package com.chencs.app.lianmeng.service;

import com.chencs.app.AppCommon;
import com.chencs.app.lianmeng.mapper.LMOrderMapper;
import com.chencs.utils.XlsUtils;
import com.chencs.vo.biz.lianmeng.LmOrderSimple;
import com.chencs.vo.biz.lianmeng.LmOrders;
import com.chencs.vo.net.lmrequest.LmOrderReportRequest;
import com.chencs.vo.net.lmrequest.LmOrderRequest;
import com.chencs.vo.net.lmresponse.LmOrderReportResponse;
import com.chencs.vo.net.lmresponse.LmOrderResponse;
import com.chencs.vo.net.response.ResponseBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LMOrderSer {



    @Autowired
    private LMOrderMapper orderMapper;

    /**
     * 获取订单
     * @param request
     * @return
     */
    public LmOrderResponse getOrders(LmOrderRequest request){
        LmOrderResponse response = new LmOrderResponse();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pageno",request.getPageNo()*request.getPageSize());
        map.put("pagesize",request.getPageSize());
        map.put("mid",request.getMid());
        if(!StringUtils.isEmpty(request.getState())){
            map.put("state",request.getState());
        }
        List<LmOrderSimple> list = orderMapper.getOrders(map);
        if(!CollectionUtils.isEmpty(list)){
            response.setOrders(list);
            response.setCode(AppCommon.SUCCESS);
            response.setMsg("获取成功!");
        }else{
            response.setCode(AppCommon.NULL);
            response.setMsg("暂无订单数据!");
        }
        return  response;
    }


    /**
     * 订单报表
     * @param request
     * @return
     */
    public LmOrderReportResponse getOrders(LmOrderReportRequest request){
        LmOrderReportResponse reportResponse = new LmOrderReportResponse();


        return  reportResponse;

    }



    /**
     * 同步订单
     * @return
     */
    public ResponseBase imporeOrder(){
        ResponseBase response = new ResponseBase();
       List<LmOrders> orders =  XlsUtils.analysisXML("C://Users//Administrator//Downloads/TaokeDetail-2018-08-04.xls",0.6);
       for(int i =0;i<orders.size();i++){
           Map<String,Object> map = new HashMap<>();
           map.put("old_price",orders.get(i).getOld_price());
           map.put("orderno",orders.get(i).getOrder_no());
           LmOrders order  = orderMapper.getOrderByNo(map);
            if(order==null){
                orderMapper.insertOrder(orders.get(i));
            }else{
                //要修改状态
            }
       }


        return  response;

    }
}
